﻿# -*- coding:utf-8 -*-
#
# models.py
# 
import datetime
#
from django.db import models
from django.contrib.auth.models import User

'''
OPERATION_KIND_CHOICES = (
    ('A', 'Arrival'),  # приход
    ('E', 'Expense'),  # расход
)

OPERATION_SPECIAL_CHOICES = (
    ('no', 'none'), # стандартная операция
    ('ec', 'e_concierge'), # выплата консьержу
    ('ac', 'a_chop'),# приход помощь
    ('aa', 'a_аpartment'),# приход от жильцов
    ('ad', 'a_debtor_аpartment'),# покрытие задолжности от жильцов
)
'''

class Apartment(models.Model):
    number = models.IntegerField(verbose_name="Номер квартиры")
    summ = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Сумма")
    phone = models.CharField(max_length=64, blank=True, verbose_name="Номер телефона")
    contact_person = models.CharField(max_length=128, blank=True, verbose_name="Контактное лицо")
    data = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="Дата")
    
    class Meta:
        ordering = ['number']
        
    def __unicode__(self):
        return "%s" % self.number


# платежи по мере поступления
class Payment(models.Model):
    apartment = models.ForeignKey(Apartment, verbose_name="Квартира")
    amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Сумма")
    data = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="Дата")    
    
    class Meta:
        pass
        
    def __unicode__(self):
        return "%s" % self.data


class MonthlyPayment(models.Model):
    apartment = models.ForeignKey(Apartment, verbose_name="Квартира")
    payment = models.ForeignKey(Payment, verbose_name="Платеж")
    year = models.IntegerField()
    month = models.IntegerField()
    amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Сумма")
    data = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="Дата")    
    
    class Meta:
        pass
        
    def __unicode__(self):
        return "%s" % self.apartment.number
       
        
class Variables(models.Model):       
    name = models.CharField(max_length=255, verbose_name="Имя переменной", unique=True)
    name_verbose = models.CharField(max_length=255, verbose_name="Переменная")
    value = models.CharField(max_length=255, verbose_name="Значение переменной")


    class Meta:
        pass
        
    def __unicode__(self):
        return "%s" % self.name
        


class Operation(models.Model):
    kind = models.CharField(max_length=1, verbose_name="Тип")
    name = models.CharField(max_length=255, verbose_name="Наименование")
    quantity = models.IntegerField(verbose_name="Количество")
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Цена")
    amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Сумма")
    comment = models.CharField(max_length=255, blank=True, verbose_name="Примечание")
    special_item = models.CharField(max_length=2, verbose_name="Специальная операция")
    period_year = models.IntegerField(verbose_name="Период - год")
    period_month = models.IntegerField(verbose_name="Период - месяц")
    data = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name="Дата")    
    
    class Meta:
        ordering = ['-data', '-kind']
        
    #def save(self, **kwargs):
    #    d = datetime.datetime.now()
    #    self.period_year = d.year
    #    self.period_month = d.month
    #    super(Operation, self).save(**kwargs)        
        
    def __unicode__(self):
        return "%s" % self.name
        
        
        
